package com.ruoyi.system.billiards.service.impl;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.billiards.service.BilliardsMemberService;
import com.ruoyi.system.domain.BilliardsMember;
import com.ruoyi.system.mapper.BilliardsMemberMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Slf4j
@Service
@RequiredArgsConstructor
public class BilliardsMemberServiceImpl implements BilliardsMemberService {

    private final BilliardsMemberMapper billiardsMemberMapper;
    @Override
    public BilliardsMember login(BilliardsMember billiardsMember) {
        BilliardsMember existMember = billiardsMemberMapper.findByOpenid(billiardsMember.getOpenid());
        if (existMember == null) {
            BilliardsMember newMember = BilliardsMember.builder()
                    .username(billiardsMember.getUsername())
                    .avatar(billiardsMember.getAvatar())
                    .openid(billiardsMember.getOpenid())
                    .build();
            newMember.setCreateTime(DateUtils.getNowDate());
            billiardsMemberMapper.insertBilliardsMember(newMember);
            return newMember;
        }
        return existMember;
    }

    @Override
    public BilliardsMember updateMember(BilliardsMember req) {
        BilliardsMember member = billiardsMemberMapper.selectBilliardsMemberByMemberId(req.getMemberId());
        if (member == null) {
            log.error("member_id:{} 未查询到相关用户", req.getMemberId());
            throw new ServiceException("参数错误");
        }
        member.setSex(req.getSex());
        member.setPhone(req.getPhone());
        member.setUsername(req.getUsername());
        member.setAvatar(req.getAvatar());
        billiardsMemberMapper.updateBilliardsMember(member);
        return member;
    }
}
